package com.yy.jhl.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;

import com.yy.common.commonutils.FileUtil;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.app.MyApplication;

import java.io.File;
import java.util.Locale;

/**
 * Created by Ellis.Li on 2017/6/23.
 */

public class DeviceUtil {
    /**
     * 获取设备的唯一标识，deviceId
     *
     * @param context
     * @return
     */
    public static String getDeviceId(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = tm.getDeviceId();
        if (deviceId == null) {
            return "";
        } else {
            return deviceId;
        }
//        String androidId = Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);
//        return androidId;
    }
    /**
     * 获取手机品牌
     *
     * @return
     */
    public static String getPhoneBrand() {
        return android.os.Build.BRAND;
    }
    /**
     * 获取手机型号
     *
     * @return
     */
    public static String getPhoneModel() {
        return android.os.Build.MODEL;
    }
    /**
     * 获取版本名称
     * @return 当前应用的版本号
     */
    public static String getVersion(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "V1.0.0";
        }
    }
    /**
     * 获取版本号
     * @return 当前应用的版本号
     */
    public static int getVersionCode(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            int version = info.versionCode;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
    /**
     * @brief 判断手机是否有SD卡
     * @return true表示有，false表示无
     */
    public static boolean hasSDCard() {
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }
    public static boolean isPathHasSpace(String path){
        long freeeSize = getUsableSpace(new File(path));
        if(freeeSize <= 0)
            return false;
        else
            return freeeSize >= 1024 * 1024;
    }
    /**
     * 获取文件路径空间大小
     * @param path
     */
    public static long getUsableSpace(File path) {
        try{
            final StatFs stats = new StatFs(path.getPath());
            return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
        }catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
    /**
     * @brief 获取应用程序外部存储区的目录
     * @return 应用程序外部存储区目录，目录以"/"结尾
     */
    public static String getExternalStorage() {
        String externalPath = Environment.getExternalStorageDirectory()
                .getAbsolutePath();
        if (externalPath == null || "".equals(externalPath.trim()) || !FileUtil.fileIsExists(externalPath)) {
            externalPath = AppConstant.DEFAULT_EXTERNAL_PATH;
        } else if (!externalPath.endsWith("/")) {
            externalPath += "/";
        }
        return externalPath;
    }

    /**
     * @brief 获取应用程序内部存储区的数据目录
     * @return 内部存储区数据目录，目录以"/"结尾
     */
    public static String getInternalStorage() {
        String internalPath = MyApplication.getInstance().getApplicationInfo().dataDir;
        if (internalPath == null || "".equals(internalPath.trim())) {
            internalPath = AppConstant.DEFAULT_INTERNAL_PATH;
        } else if (!internalPath.endsWith("/")) {
            internalPath += "/";
        }
        return internalPath;
    }
    /**
     * @brief 获取内部存放文件的目录
     * @detail /data/data/com.gnet.uc/files/
     */
    public static String getInternalFilesDir(){
        File dir = MyApplication.getInstance().getFilesDir();
        if(dir == null || !dir.exists()){
            dir = new File(getInternalStorage(), "files");
            if(!dir.exists()){
                dir.mkdirs();
            }
        }
        String filesDir = dir.getAbsolutePath();
        if (!filesDir.endsWith("/")) {
            filesDir += "/";
        }
        return filesDir;
    }
    public static boolean isZh(Context context) {
        Locale locale = context.getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        if (language.endsWith("zh"))
            return true;
        else
            return false;
    }
}
